//
//  WorkflowMenuView.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 2017/8/24.
//  Copyright © 2017年 com.xtion.crm. All rights reserved.
//

#import "WorkflowMenuView.h"

@interface WorkflowMenuView()<XUIButtonDelegate>
{
    UIImageView *_headImageView;
    UILabel *_titleLabel;
    UIView *_blankLineView;
    
    XUIButton *_allBtn;
    XUIButton *_toDoBtn;
    XUIButton *_applyBtn;
}
@end

@implementation WorkflowMenuView

@synthesize szTag;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.userInteractionEnabled = NO;
        
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        
        self.backgroundColor = [UIColor redColor];
        
        _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 45, 45)];
        _headImageView.layer.cornerRadius = 5;
        _headImageView.clipsToBounds = YES;
        [self addSubview:_headImageView];
        
        CGSize s = [NSString charSizeWithSystemFontSize:16];
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, s.width*8, s.height)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        [self addSubview:_titleLabel];
        
        _blankLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 0.5)];
        _blankLineView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_blankLineView];
        
        _allBtn = [XUIButton buttonWithStyleType:XUIButtonStyleTypeLeftHeadImage delegate:self];
        _allBtn.size = CGSizeMake(50, 30);
        [_allBtn SetTitleColor:[UIColor whiteColor]];
        [_allBtn SetTitle:@"全部"];
        [_allBtn SetFont:[UIFont systemFontOfSize:13]];
        _allBtn.layer.cornerRadius = 5;
        _allBtn.layer.masksToBounds = YES;
        [self addSubview:_allBtn];
        
        _toDoBtn = [XUIButton buttonWithStyleType:XUIButtonStyleTypeLeftHeadImage delegate:self];
        _toDoBtn.size = CGSizeMake(50, 30);
        [_toDoBtn SetTitleColor:[UIColor whiteColor]];
        [_toDoBtn SetTitle:@"待办"];
        [_toDoBtn SetFont:[UIFont systemFontOfSize:13]];
        _toDoBtn.layer.cornerRadius = 5;
        _toDoBtn.layer.masksToBounds = YES;
        [self addSubview:_toDoBtn];
        
        _applyBtn = [XUIButton buttonWithStyleType:XUIButtonStyleTypeLeftHeadImage delegate:self];
        _applyBtn.size = CGSizeMake(50, 30);
        [_applyBtn SetTitleColor:[UIColor whiteColor]];
        [_applyBtn SetTitle:@"发起"];
        [_applyBtn SetFont:[UIFont systemFontOfSize:13]];
        _applyBtn.layer.cornerRadius = 5;
        _applyBtn.layer.masksToBounds = YES;
        [self addSubview:_applyBtn];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame userInteractionEnabled:(BOOL)userInteractionEnabled
{
    self = [self initWithFrame:frame];
    
    self.userInteractionEnabled = userInteractionEnabled;
    
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    _blankLineView.top = _titleLabel.bottom + 5;
    _blankLineView.width = self.width;
    
    _headImageView.left = _titleLabel.left;
    _headImageView.top = self.height - _headImageView.width - 10;
    
    _allBtn.left = self.width - 10 - _allBtn.width;
    _allBtn.top = _blankLineView.bottom + 5;
    
    _toDoBtn.left = _allBtn.left;
    _toDoBtn.top = _allBtn.bottom + 5;
    
    _applyBtn.left = _allBtn.left;
    _applyBtn.top = _toDoBtn.bottom + 5;
}

-(void)setHeadImgUrl:(NSString *)headImgUrl
{
    UIImage *headImage = nil;
    if(headImgUrl.length >0)
    {
        if([headImgUrl rangeOfString:ServiceDefaultHeadImageTag].location == NSNotFound)
        {
            XLGHeadImageCache *cache = [XLGHeadImageDownloadQueue checkOrStartDownloadHeadImageWhenAddHeadImageURL:headImgUrl forKey:XLGHeadImageDownloadQueueSysIcon];
            if(cache != nil)
                headImage = cache;
            else
                headImage = [UIImage imageNamed:@"Icon"];
        }
        else
        {
            NSString *h = [headImgUrl substringFromIndex:[headImgUrl rangeOfString:ServiceDefaultHeadImageTag].length];
            headImage = [UIImage imageNamed:h];
        }
    }
    else
        headImage = [UIImage imageNamed:@"Icon"];
    
    headImage = [UIImage scaleImage:headImage size:CGSizeMake(45.0,45.0)];
    
    _headImageView.image = headImage;
}

-(void)setTitle:(NSString *)title
{
    _titleLabel.text = title;
}

-(void)clickedUpInsideOfButton:(XUIButton *)btn
{
    if(btn == _allBtn)
    {
        if([_delegate respondsToSelector:@selector(clickWorkflowMenuViewToDoBtn:)])
            [_delegate clickWorkflowMenuViewAllBtn:self];
    }
    else if(btn == _toDoBtn)
    {
        if([_delegate respondsToSelector:@selector(clickWorkflowMenuViewToDoBtn:)])
            [_delegate clickWorkflowMenuViewToDoBtn:self];
    }
    else if(btn == _applyBtn)
    {
        if([_delegate respondsToSelector:@selector(clickWorkflowMenuViewApplyBtn:)])
            [_delegate clickWorkflowMenuViewApplyBtn:self];
    }
}

#pragma mark --
#pragma mark -- refresh headImage
-(void)observerHeadImageDownloadQueueHeadImageDownloadResultNotification:(NSNotification*)noti
{
    NSString *path =[noti.userInfo objectForKey:XLGHeadImageDownloadQueueHeadImageDownloadResultPathKey];
    NSString *URL =[noti.userInfo objectForKey:XLGHeadImageDownloadQueueHeadImageDownloadResultURLKey];
    
    XLGHeadImageCache *cache = [XLGHeadImageDownloadQueue setCacheWithHeadImageURL:URL andHeadImagePath:path forKey:XLGHeadImageDownloadQueueSysIcon];
    
    UIImage *headImage = (UIImage*)cache;
    headImage = [UIImage scaleImage:headImage size:CGSizeMake(45.0,45.0)];
    
    _headImageView.image = headImage;
}
@end
